package com.np.adapterdemo2;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.np.adapterdemo2.pojo.Blog;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null && result.getResultCode() == RESULT_OK && result.getData() != null) {
                    Blog entity = (Blog) result.getData().getSerializableExtra("entity");
                    if (entity != null) {
                        String message = String.format("你选择了： %s", entity.getTitle());
                        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        findViewById(R.id.button_01).setOnClickListener(v -> launcher.launch(new Intent(this, BaseAdapterNoLayoutActivity.class)));
        findViewById(R.id.button_02).setOnClickListener(v -> launcher.launch(new Intent(this, BaseAdapterActivity.class)));
        findViewById(R.id.button_03).setOnClickListener(v -> launcher.launch(new Intent(this, RecyclerViewActivity.class)));
        findViewById(R.id.button_04).setOnClickListener(v -> {
            ChoseBlogDialog.showDialog(this, new ChoseBlogDialog.ButtonClickedCallback() {
                @Override
                public void onClicked(boolean isLeft, ChoseBlogDialog dialog) {
                    dialog.dismiss();
                }
            });
        });
    }
}